C++ では、抽象的な値から実行へと至るプロセスは、 ステートメントから始まります。 式ステートメント は、式の末尾にセミコロンを追加することで簡単に作成されます。これによりコンパイラはその式を評価し、 制御フロー 順次進行します。
1. ヌルステートメント
「 ヌルステートメント (;)」は、言語上ステートメントが必要だが、ロジック上不要な場合に使用するプレースホルダーです。一部のループで有用ですが、 余分なヌルステートメント——たとえば、 while または if ヘッダの後に誤ってセミコロンが入ると、意図した本体が無視される深刻な論理エラーを引き起こす可能性があります。
⚠️ 注意(p. 235): ループヘッダの後に誤ってセミコロンを書くと、本体としてヌルステートメントが作られ、多くの場合無限ループになります。
2. 組み合わせステートメント(ブロック)
「 組み合わせステートメント、あるいは ブロックとは、波括弧「{ }」で囲まれた複数のステートメントの連なりです。これは単一の実行ユニットとして扱われます。ブロックは独自のスコープを持ち、内部で定義された名前は外部からは見えません。 { }
注意(p. 235): 単純なステートメントとは異なり、ブロックは セミコロンで終了しません。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which of the following creates an 'expression statement'?
An expression followed by a semicolon.
Any line of code within curly braces.
A variable declaration without an assignment.
A comment followed by a semicolon.
✅ Correct!
Correct. In C++, adding a semicolon to an expression turns it into a statement.❌ Incorrect
An expression statement specifically refers to an expression terminated by a semicolon.QUESTION 2
Exercise 5.2: What is a block and when might you use one?
A block is a group of functions; used to organize large projects.
A block is a single semicolon; used to end a program.
A block is a sequence of statements in braces; used where the syntax requires one statement but logic requires many.
A block is a reserved memory segment; used for global variables.
✅ Correct!
Precisely. A block (compound statement) allows multiple statements to be treated as one, which is essential for control structures like 'if' and 'while'.❌ Incorrect
Recall that a block is defined by { } and acts as a single execution unit.QUESTION 3
What is the primary danger of an 'extraneous null statement' after a while loop header?
The compiler will throw a syntax error.
The loop body will be treated as the null statement, causing the intended logic to execute only once or never.
It automatically converts the loop into a do-while loop.
It speeds up the execution by skipping checks.
✅ Correct!
Correct. If you write 'while(cond); { body }', the semicolon IS the body, and the block in braces is just a separate statement executed after the loop finishes.❌ Incorrect
The compiler sees it as valid syntax, but it changes the logic entirely—the semicolon becomes the loop's body.QUESTION 4
Exercise 5.3: Does using the comma operator to compress a while loop into a single statement without a block improve readability?
Yes, less lines always means clearer code.
No, it typically diminishes readability by hiding the loop's intent and side effects.
Yes, it is the standard way to write C++ loops.
No, the comma operator cannot be used in while loops.
✅ Correct!
Correct. While syntactically valid, squeezing logic into the loop header using commas makes the code harder to debug and understand.❌ Incorrect
Readable code clearly separates condition checking from the actions taken. Comma operators can obscure this.QUESTION 5
Which statement about block termination is correct?
Every block must end with a semicolon after the closing brace.
A block is NOT terminated by a semicolon.
Only blocks inside functions require a semicolon.
A block must end with a null statement.
✅ Correct!
Correct. The closing brace itself signals the end of the compound statement.❌ Incorrect
Adding a semicolon after a closing brace creates an unnecessary null statement.Statement Logic & Factorials
Applying anatomy of statements to algorithmic logic.
You are tasked with implementing a factorial function and a vowel counter. You must ensure proper scoping and avoid syntax-induced logic errors.
Q
1. [Writing Task] Write your own version of the factorial function ('fact') using a while loop. (Difficulty: Medium, Length: 75 words)
Solution:
A robust version of factorial should use a 'long long' to prevent overflow:
A robust version of factorial should use a 'long long' to prevent overflow:
long long fact(int val) {
long long res = 1;
while (val > 1)
res *= val--; // Expression statement
return res;
}
This function uses a single expression statement as the loop body. A block isn't strictly required here, but could be added for clarity. We decrement 'val' within the expression to move toward the termination condition.Q
2. [Writing Task] Exercise 5.10: Implement a logic block that counts both lower- and uppercase vowels in a string using a switch statement.
Solution:
To count both cases, we stack case labels to share a single block of code:
To count both cases, we stack case labels to share a single block of code:
switch (ch) {
case 'a': case 'A':
++aCnt;
break;
case 'e': case 'E':
++eCnt;
break;
// ... repeat for i, o, u
}
By omitting 'break' between 'a' and 'A', the flow of control falls through, effectively counting both characters toward the same counter.